package com.app.controller.backstage;

import com.app.common.Result;
import com.app.model.ActivityModel;
import com.app.model.AdminUserModel;
import com.app.model.StudyModel;
import com.app.service.ActivityAdminService;
import com.app.service.AdminUserService;
import com.app.service.StudyAdminService;
import com.app.service.StudyService;
import com.app.util.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@Controller
@RestController
@RequestMapping("/admin/study")
public class StudyAdminController {
    @Autowired
    private StudyAdminService studyAdminService;
    @Autowired
    private StudyService studyService;
    @Autowired
    private AdminUserService adminUserService;

    //分类列表
    @GetMapping("/selectTypeList")
    public Result<Object> selectTypeList() {
        return Result.success(studyService.selectTypeList());
    }

    //增加和编辑
    @PostMapping("/insertAndUpdateStudy")
    public Result<Object> insertAndUpdateStudy(@RequestBody StudyModel Study,HttpServletRequest request) {
        String token = request.getHeader("authori-zation");
        String userId = TokenUtils.nothingness(token);
        if (userId == null) {
            return Result.error(405, "token不存在");
        }

        AdminUserModel user = adminUserService.getUserById(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(405, "用户不存在");
        }
        if (user.getVillageId() != 0) {
            Study.setVillageId(user.getVillageId());
        } else {
            Study.setVillageId(0);
        }
        if (Study.getId() == 0) {
            studyAdminService.insert(Study);
        }
        studyAdminService.updateById(Study);
        return Result.success("操作成功");
    }

    //删除
    @GetMapping("/deleteStudyById")
    public Result<Object> deleteStudyById(Integer id) {
        if (id != null && id != 0) {
            studyAdminService.deleteStudyById(id);
        }
        return Result.success("删除成功");
    }

    //查询在线学习列表
    @GetMapping("/selectStudyList")
    public Result<Object> selectStudyList(Integer villageId, Integer page, Integer limit, String title, HttpServletRequest request) {
        String token = request.getHeader("authori-zation");
        String userId = TokenUtils.nothingness(token);
        if (userId == null) {
            return Result.error(405, "token不存在");
        }

        AdminUserModel user = adminUserService.getUserById(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(405, "用户不存在");
        }
        if (user.getVillageId() == 0 && villageId != null) {
            user.setVillageId(villageId);
        }
        return Result.success(studyAdminService.selectStudyList(page, limit, title,user.getVillageId()));
    }

    //修改状态
    @GetMapping("/updateStatus")
    public Result<Object> updateStatus(Integer id, Integer status) {
        StudyModel model = new StudyModel();
        model.setId(id);
        model.setStatus(status);
        studyAdminService.updateById(model);
        return Result.success("操作成功");
    }
}